package com.changgou.service.goods.controller;

import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Goods;
import com.changgou.service.goods.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author tu zy
 * @version 1.0
 * @date 2021/6/22 22:57
 */
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Autowired
    private SpuService spuService;

    /**
     * 通过spuId查询商品goods
     * @param spuId
     * @return
     */
    @GetMapping("/goods/{spuId}")
    public Result<Goods> findGoods(@PathVariable("spuId") Long spuId){
        Goods goods = spuService.findGoodsById(spuId);
        return new Result<>(true, StatusCode.OK,"查询商品成功",goods);
    }

    /**
     * 增加商品实现
     */
    @PostMapping("/save")
    public Result saveGoods(@RequestBody Goods goods ){
        spuService.saveGoods(goods);

        return new Result(true,StatusCode.OK,"商品增加成功");
    }

    /**
     * 商品审核
     */
    @PutMapping("/audit/{spuId}")
    public Result auditGoods(@PathVariable("spuId") Long spuId){
        spuService.audit(spuId);

        return new Result(true,StatusCode.OK,"商品审核成功");
    }

    /**
     * 商品下架
     */
    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable Long id){
        spuService.pull(id);
        return new Result(true,StatusCode.OK,"下架成功");
    }

    /**
     * 商品上架
     */
    @PutMapping("/put/{id}")
    public Result put(@PathVariable Long id){
        spuService.put(id);
        return new Result(true,StatusCode.OK,"上架成功");
    }

    /**
     * 批量上架
     */
    /**
     *  批量上架
     * @param ids
     * @return
     */
    @PutMapping("/put/many")
    public Result putMany(@RequestBody Long[] ids){
        int count = spuService.putMany(ids);
        return new Result(true,StatusCode.OK,"上架"+count+"个商品");
    }




}
